Double

您所在的位置:网站首页 garbled byte Double

Double

2023-07-03 19:50| 来源: 网络整理| 查看: 265

A matplotlib chart and garbled two-byte characters on a blackboard

I' ve been bothered by garbled double-byte fonts in matplotlib-generated PDFs, probably since I upgraded my Mac's OS to Ventura 13. What makes matters more complicated, characters are garbled when viewed in Adobe Acrobat Pro & Reader but look totally OK in Preview.app. This issue doesn't seem limited to Japanese fonts but common among double-byte fonts such as Chinese and Korean ones. I wrote about a makeshift solution before, but this time I came up with a better one.

Double-byte Fonts Garbled in Mac matplotlib-Generated PDFs

In Mac matplotlib-generated PDFs, double-byte characters are unreadable in Adobe Acrobat Reader while they look fine in Preview.app.

A Bit Technical

Environment: macOS Ventura 13.1, miniconda 22.11.1, Python 3.11.0, numpy 1.24.1, matplotlib 3.6.3

Problem

These figures represent the issue. When I used a double-byte Japanese font, Hiragino Sans, to generate a figure, (1) it was OK in Preview, but (2) all characters, including alphabets and numerals, were garbled in Acrobat. This was also the case with a traditional Chinese font (繁體字), PingFang HK (3,4). Then I assigned a single-byte font, Helvetica, for ASCII characters and found that (5) the PDF was again fine in Preview. In Acrobat, however, double-byte-font characters were missing, while alphabets and numerals appeared normal (6).

1. Hiragino, Preview2. Hiragino, Acrobat3. PinFang, Preview4. PinFang, Acrobat5. Helvetica + Hiragino, Preview6. Helvetica + Hiragino, AcrobatFont settings and appearance in Acrobat/Preview

This is the Python code for the above plots.

import numpy as np from matplotlib import pyplot as plt from matplotlib import rcParams rcParams['pdf.fonttype'] = 42 rcParams['font.family'] = 'Hiragino Sans' # To assign a single-byte font for alphabets and numerals: rcParams['font.family'] = ['Helvetica', 'Hiragino Sans'] x = np.linspace(-np.pi,np.pi) y = np.sin(x) fig, ax = plt.subplots(figsize=(2,1.5)) ax.plot(x,y) ax.set_xlabel('これがX軸') ax.set_ylabel('これがY軸') ax.set_title('正弦曲線') plt.tight_layout() fig.savefig(f"{'-'.join(rcParams['font.family'] )}.pdf")

My research has revealed that all Japanese fonts that come with macOS are incompatible with Acrobat when used in matplotlib-generated PDFs. Furthermore, this problem is not limited to Japanese fonts and is shared by at least one Chinese font, PingFang.

Hiragino Maru Gothic ProHiragino Mincho ProNHiragino Sans CNSHiragino Sans GBToppan Bunkyu GothicToppan Bunkyu Midashi GothicToppan Bunkyu Midashi MinchoToppan Bunkyu MinchoTsukushi A Round GothicTsukushi B Round GothicYuGothicYuMinchoPingFang HK

The issue was reproducible when I clean-installed the latest macOS to another machine and created a new user account and a minimal virtual environment of miniconda with just Python, numpy, and matplotlib. So, something in macOS Ventura, matplotlib, or their default settings should be responsible, not the particular settings of my Mac.

Solution

Acrobat displayed a figure with correct characters when I installed a Japanese font IPAex and set it for double-byte characters in a Python code. Try a font of your tongue if you speak another language.

IPA Font ダウンロード | 一般社団法人 文字情報技術促進協議会 一般社団法人 文字情報技術促進協議会 | Japanese Character Information Technology Promotion Council Steps I tookDownload the fontUnzip the archive and copy the .ttf file to /Library/Fonts or ~/Library/FontsDelete matplotlib's old font list by typing in your terminal rm -f ~/.matplotlib/fontlist-v330.json.In a Python code, set the IPAex font for double-byte characters. In the above example, Line 6 should be something like rcParams['font.family'] = ['Helvetica', 'IPAexGothic'].FacebooktwitterHatenaPocketRelated postsA matplotlib chart and garbled two-byte characters on a blackboard解決!Mac matplotlibの日本語PDFがAcrobatで文字化け 2023-01-26You Need to Replace Miniforge Version of Miniconda to Get Latest one 2023-01-23Mac Minicondaのアップデートが掛からないのはminiforge版だからだった 2023-01-23JupyterHow to set up Jupyter on Visual Studio Code - A comprehensive guide with sample projects 2023-01-22MacBookThe Top 10 Reasons Why Non-Programmers Should Learn Python 2023-01-20Digital Rain. Jahobr, CC0, via Wikimedia Commons. https://upload.wikimedia.org/wikipedia/commons/c/cc/Digital_rain_animation_medium_letters_shine.gifDouble-byte Fonts Garbled in Mac matplotlib-Generated PDFs 2022-12-08Digital Rain. Jahobr, CC0, via Wikimedia Commons. https://upload.wikimedia.org/wikipedia/commons/c/cc/Digital_rain_animation_medium_letters_shine.gifMacのmatplotlibで日本語を含むグラフPDFが文字化け 2022-12-07Base64, PyScriptとWordPress 2022-06-27Base64, PyScript, and WordPress 2022-06-27Brief Review of PyScript on WordPress 2022-05-09


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3